package cj.web.admin.controllers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Flash;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.GeTui;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.AuditStepLogDAO;
import cj.web.admin.dao.ColumnsDAO;
import cj.web.admin.dao.NewsAuditDAO;
import cj.web.admin.dao.NewsDAO;
import cj.web.admin.dao.NewsVersionDAO;
import cj.web.admin.dao.SubjectDAO;
import cj.web.admin.domain.AuditStep;
import cj.web.admin.domain.AuditStepConfig;
import cj.web.admin.domain.AuditStepLog;
import cj.web.admin.domain.Columns;
import cj.web.admin.domain.News;
import cj.web.admin.domain.NewsVersion;
import cj.web.admin.domain.Subject;
import cj.web.constants.AuditStepAgreeEnum;
import cj.web.constants.MemuEnum;
import cj.web.constants.NewsCheckStatus;
import cj.web.constants.NewsLabelEnum;
import cj.web.constants.NewsReleaseStateEnum;
import cj.web.constants.NewsTypeEnum;
import cj.web.login.controllers.BaseController;

@Path("/news")
public class NewsController extends BaseController {
	
	@Autowired
	private NewsDAO newsDAO;
	@Autowired
	private NewsAuditDAO newsAuditDao;
	@Autowired
	private ColumnsDAO columnsDao;
	@Autowired
	private SubjectDAO subjectDAO;
	@Autowired
	private AuditStepLogDAO auditStepLogDao;
	@Autowired
	private NewsVersionDAO newsVersionDao;
	
	@Get("show")
	public String show(Model model, HttpServletRequest request){
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());//根据账号获取栏目
		model.add("Columnslis",columnslis);

		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdcgx);
		return "/views/news/newsList.vm";
	}
	
	@Post("getList")
    public String getList(News news, HttpServletRequest request){
		news.setCreateUser(returnSessionObj(request).getAccount());
		List<News> arr = newsDAO.getList(news);
		JSONObject jo = new JSONObject();
		
		int size = newsDAO.getListSize(news);
		
		jo.put("data", arr);
		jo.put("draw", news.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	/**
	 * 发送推送功能
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("showNewsSendMsgList")
	public String showNewsSendMsgList(Model model, HttpServletRequest request,@Param("currentPage") int currentPage){
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());//根据账号获取栏目
		model.add("Columnslis",columnslis);

		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdtsxw);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/newsSendMsgList.vm";
	}
	
	/**
	 * 发送推送列表方法
	 * @param news
	 * @param request
	 * @return
	 */
	@Post("getNewsSendMsgList")
    public String getNewsSendMsgList(News news, HttpServletRequest request){
		
		List<News> arr = newsDAO.getUpNewsList(news);
		
		JSONObject jo = new JSONObject();
		
		int size = newsDAO.getUpNewsListSize(news);
		
		jo.put("data", arr);
		jo.put("draw", news.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	/**
	 * 修改新闻列表
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("showUpNewsList")
	public String showUpNewsList(Model model, HttpServletRequest request,@Param("currentPage") int currentPage){
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());//根据账号获取栏目
		model.add("Columnslis",columnslis);

		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdxgxw);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/newsUpList.vm";
	}
	
	/**
	 * 修改新闻列表方法
	 * @param news
	 * @param request
	 * @return
	 */
	@Post("getUpNewsList")
    public String getUpNewsList(News news, HttpServletRequest request){
		
		List<News> arr = newsDAO.getUpNewsList(news);
		
		JSONObject jo = new JSONObject();
		
		int size = newsDAO.getUpNewsListSize(news);
		
		jo.put("data", arr);
		jo.put("draw", news.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("editUpNews")
	public String editUpNewsById(Model model,@Param("id") int id,HttpServletRequest request,@Param("currentPage") int currentPage){
		News news = newsDAO.queryById(id);
		
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());
		model.add("Columnslis",columnslis);
		if (columnslis != null && columnslis.size() > 0) {
			List<Integer> colIds = new ArrayList<Integer>();
			for (Columns col : columnslis) {
				colIds.add(col.getId());
			}
			if(colIds.size() > 0){
				List<Subject> subjects = subjectDAO.queryByColIds(colIds);
				model.add("Subjects",subjects);
			}
		}
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		
		if(news!=null){
			news.setReleaseAt(DateUtils.getMinusTime(news.getReleaseAt(), DateUtils.DATE_FULL_STR, 0));
			model.add("obj", news);
		}
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdxgxw);
        model.add("currentPage",currentPage);//当前页
        model.add("NewsLabelEnum",EnumSet.allOf(NewsLabelEnum.class));//新闻lable
        
		return "/views/news/newsUpEdit.vm";
	}
	
	@Post("updateUpNews")
	public String updateUpNewsById(News news,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		news.setStatus(NewsCheckStatus.SHTG.getValue());
		news.setUpdateAt(DateUtils.getNowTime());
		news.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			
			newsDAO.update(news);
			
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	//审核详情
	@Get("auditInfor")
	public String auditById(Model model,@Param("id") int id,HttpServletRequest request){
		News news = newsDAO.queryById(id);
		
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());
		model.add("Columnslis",columnslis);
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		
		if(news!=null){
			news.setReleaseAt(DateUtils.getMinusTime(news.getReleaseAt(), DateUtils.DATE_FULL_STR, 0));
			model.add("obj", news);
		}
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwddwsh);
        
        List<AuditStepLog> auditStepLog = auditStepLogDao.queryByNewsId(id);
        for(AuditStepLog obj:auditStepLog)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}
        model.add("auditStepLog",auditStepLog);
        
		return "/views/news/newsAuditDetail.vm";
	}
	
	//审核步骤详情
	@Get("auditDetailInfor")
	public String inforById(Model model,@Param("id") int id,HttpServletRequest request){
		
		AuditStepLog auditStepLog = auditStepLogDao.queryById(id);
		model.add("auditStepLog",auditStepLog);
		
		NewsVersion news = newsVersionDao.queryByAuditStepLogId(id);
		if(news!=null){
			news.setReleaseAt(DateUtils.getMinusTime(news.getReleaseAt(), DateUtils.DATE_FULL_STR, 0));
			model.add("obj", news);
		}
		
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());
		model.add("Columnslis",columnslis);
		
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwddwsh);

        return "/views/news/newsAuditDetailInfor.vm";
	}
	
	@Get("createPage")
	public String createPage(Model model, HttpServletRequest request){
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());//根据账号获取栏目
		if (columnslis != null && columnslis.size() > 0) {
			List<Integer> colIds = new ArrayList<Integer>();
			for (Columns col : columnslis) {
				colIds.add(col.getId());
			}
			if(colIds.size() > 0){
				List<Subject> subjects = subjectDAO.queryByColIds(colIds);
				model.add("Subjects",subjects);
			}
		}
		model.add("Columnslis",columnslis);
		model.add("NewsLabelEnum",EnumSet.allOf(NewsLabelEnum.class));//新闻lable
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdcgx);
		return "/views/news/newsDetail.vm";
	}
	
	@Post("create")
	public String create(Model model, News news,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		news.setAuthor(returnSessionObj(request).getName());//作者
		news.setIsTop(Constants.flag.type0);//不是头条
		news.setIsComments(Constants.flag.type1);//可以评论
		news.setPvNum(0);//pv访问量为0
		news.setPraiseNum(0);//点赞数为0
		news.setCommentsNum(0);//评论数为0
		news.setForwardNum(0);//转发量为0
		news.setCollectNum(0);;//收藏量为0
		news.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		news.setCreateAt(DateUtils.getNowTime());
		news.setCreateUser(returnSessionObj(request).getAccount());
		news.setUpdateAt(DateUtils.getNowTime());
		news.setUpdateUser(returnSessionObj(request).getAccount());
		news.setPvNumFinal(0);//pv实际访问量为0
		try{
			
			if(NewsReleaseStateEnum.CAOGAO.getValue()==news.getReleaseState())
				//草稿
			{
				news.setStatus(NewsCheckStatus.WTJ.getValue());//审核状态 - 未提交
			}
			else if(NewsReleaseStateEnum.SHZ.getValue()==news.getReleaseState())
				//待审核
			{
				int auditConfigSize = newsAuditDao.getAuditConfigSize();
				if(auditConfigSize>0)
					//存在审核人员
				{
					news.setStatus(NewsCheckStatus.DSH.getValue());//审核状态 - 待审核
				}
				else
					//不存在审核人员
				{
					news.setReleaseState(NewsReleaseStateEnum.YFB.getValue());//发布状态 - 已发布
					news.setStatus(NewsCheckStatus.SHTG.getValue());//审核状态 - 审核通过
				}
			}
			
			int newsId = newsDAO.create(news);
			news.setId(newsId);
			createAuditStep(news, request);
			
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
		
	}
	
	/**
	 * 创建审核记录
	 * @param news		新闻Obj
	 * @param request	http操作对象
	 */
	private void createAuditStep(News news,HttpServletRequest request)
	{
		//创建审核记录
		if(NewsCheckStatus.DSH.getValue()==news.getStatus())
		{
			/***
			 * 以下是Step by Step 审核方式，保留
			//是否存在第一个审核人
			AuditStepConfig obj = newsAuditDao.getAuditConfigByStepBegin(0);//查询第一个审核人
			
			if(obj!=null)
			{
				AuditStep auditStep = new AuditStep();
				auditStep.setNewsId(news.getId());
				auditStep.setStepBegin(obj.getStepBegin());
				auditStep.setStepEnd(obj.getStepEnd());
				auditStep.setCreateUser(returnSessionObj(request).getAccount());
				auditStep.setUpdateUser(returnSessionObj(request).getAccount());
				auditStep.setUserId(obj.getUserId());
				auditStep.setName(obj.getName());
				auditStep.setIsAgree(AuditStepAgreeEnum.DDCZ.getValue());
				auditStep.setMemo("");
				newsAuditDao.createAuditStep(auditStep);
			}
			*/
			
			//是否存在审核人
			List<AuditStepConfig> objls = newsAuditDao.getAuditConfigAll();
			
			for(AuditStepConfig obj:objls){
				
				AuditStep auditStep = new AuditStep();
				auditStep.setNewsId(news.getId());
				auditStep.setStepBegin(obj.getStepBegin());
				auditStep.setStepEnd(obj.getStepEnd());
				auditStep.setCreateUser(returnSessionObj(request).getAccount());
				auditStep.setUpdateUser(returnSessionObj(request).getAccount());
				auditStep.setUserId(obj.getUserId());
				auditStep.setName(obj.getName());
				auditStep.setIsAgree(AuditStepAgreeEnum.DDCZ.getValue());
				auditStep.setMemo("");
				newsAuditDao.createAuditStep(auditStep);
			}
		}
	}
		
	@Get("deleteById")
	public String deleteById(@Param("id") int id)
	{
		newsDAO.deleteById(id);
		return "r:show";
	}
	
	@Get("edit")
	public String editById(Model model,@Param("id") int id,HttpServletRequest request){
		News news = newsDAO.queryById(id);
		
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());
		model.add("Columnslis",columnslis);
		if (columnslis != null && columnslis.size() > 0) {
			List<Integer> colIds = new ArrayList<Integer>();
			for (Columns col : columnslis) {
				colIds.add(col.getId());
			}
			if(colIds.size() > 0){
				List<Subject> subjects = subjectDAO.queryByColIds(colIds);
				model.add("Subjects",subjects);
			}
		}
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		
		if(news!=null){
			if(news.getReleaseAt()!=null && !"".equals(news.getReleaseAt()))
			{
				news.setReleaseAt(DateUtils.getMinusTime(news.getReleaseAt(), DateUtils.DATE_FULL_STR, 0));
			}
			
			model.add("obj", news);
		}
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdcgx);
        model.add("NewsLabelEnum",EnumSet.allOf(NewsLabelEnum.class));//新闻lable
		return "/views/news/newsEdit.vm";
	}
	
	@Post("update")
	public String updateById(News news,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		news.setUpdateAt(DateUtils.getNowTime());
		news.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			
			if(NewsReleaseStateEnum.CAOGAO.getValue()==news.getReleaseState())
				//草稿
			{
				news.setStatus(NewsCheckStatus.WTJ.getValue());//审核状态 - 未提交
			}
			else if(NewsReleaseStateEnum.SHZ.getValue()==news.getReleaseState())
				//待审核
			{
				int auditConfigSize = newsAuditDao.getAuditConfigSize();
				if(auditConfigSize>0)
					//存在审核人员
				{
					news.setStatus(NewsCheckStatus.DSH.getValue());//审核状态 - 待审核
				}
				else
					//不存在审核人员
				{
					news.setReleaseState(NewsReleaseStateEnum.YFB.getValue());//发布状态 - 已发布
					news.setStatus(NewsCheckStatus.SHTG.getValue());//审核状态 - 审核通过
				}
			}
			
			
			newsDAO.update(news);
			
			createAuditStep(news, request);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
		
	}
	
	@Get("view")//预览
	public String view(Model model,@Param("id") int id,HttpServletRequest request){
		/*News news = newsDAO.queryById(id);
		
		if(news!=null){
			String releaseAt = news.getReleaseAt();
			if( releaseAt != null && !"".equals(releaseAt)){
				news.setReleaseAt(DateUtils.getMinusTime(releaseAt, DateUtils.DATE_FULL_STR, 0));
			}
			
			model.add("obj", news);
		}
		
		List<Columns> columnslis = columnsDao.queryByAccountForColumns(returnSessionObj(request).getAccount());
		model.add("Columnslis",columnslis);
		
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwddwsh);
		
		return "/views/news/newsView.vm";*/
		
		return "r:" + shareUrl+id;
	}
	
	@Get("copy")//复制
	public String copy(@Param("id") int id,Flash flash, HttpServletRequest request, @Param("currentPage")int currentPage)
	{
		News news = newsDAO.queryById(id);
		if(news != null){
			news.setPvNumFinal(0);
			news.setPvNum(0);//pv访问量为0
			news.setPraiseNum(0);//点赞数为0
			news.setCommentsNum(0);//评论数为0
			news.setForwardNum(0);//转发量为0
			news.setCollectNum(0);;//收藏量为0
			news.setReleaseState(NewsReleaseStateEnum.CAOGAO.getValue());//发布状态 - 草稿
			news.setStatus(NewsCheckStatus.DSH.getValue());//审核状态 - 待审核
			news.setIsTop(Constants.flag.type0);//不是头条
			news.setIsComments(Constants.flag.type1);//可以评论
			news.setCreateAt(DateUtils.getNowTime());//创建时间
			news.setCreateUser(returnSessionObj(request).getAccount());//创建人
			news.setUpdateAt(DateUtils.getNowTime());//更新时间
			news.setUpdateUser(returnSessionObj(request).getAccount());//更新人
			news.setId(null);
			newsDAO.create(news);
		}
		
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Get("down")//撤稿
	public String down(@Param("id") int id, Flash flash, HttpServletRequest request,@Param("currentPage") int currentPage)
	{
		News news = newsDAO.queryById(id);
		
		news.setReleaseState(NewsReleaseStateEnum.CG.getValue());//发布状态 - 撤稿
		news.setStatus(NewsCheckStatus.DSH.getValue());//审核状态 - 待审核
		news.setIsTop(Constants.flag.type0);//不是头条
		news.setIsComments(Constants.flag.type1);//可以评论
		news.setUpdateAt(DateUtils.getNowTime());//更新时间
		news.setUpdateUser(returnSessionObj(request).getAccount());//更新人
		
		if(news != null){
			newsDAO.down(news);
		}
		
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Post("upload")
	public String upload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (!file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                //扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
                
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	//扩展名  不为gif
                	ImageUtils.scale3(path+"/"+fileName, path+"/y_"+fileName, 460, 640);// 按比例压缩,做适当截取
                    
                    ImageUtils.scale3(path+"/"+fileName, path+"/l_"+fileName, 368, 640);// 按比例压缩,做适当截取
                    
                    ImageUtils.scale2(path+"/l_"+fileName, path+"/s_"+fileName, 368, 640, true);//缩率图		750*432
                    
                    ImageUtils.scale2(path+"/y_"+fileName, path+"/ss_"+fileName, 130, 181, true);//缩率图		181*130
                    
                    ImageUtils.scale2(path+"/y_"+fileName, path+"/sss_"+fileName, 165, 230, true);//缩率图	230*165
                }
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	@Get("checking")//待审核
	public String checking(Model model, HttpServletRequest request){
		List<Columns> columnslis = columnsDao.queryAll();//根据账号获取栏目
		model.add("Columnslis",columnslis);

		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwddsh);
		return "/views/news/newsCheckList.vm";
	}
	
	@Get("released")//已发布
	public String released(Model model, Flash flash, @Param("currentPage") int currentPage,HttpServletRequest request){
		flash.add("returnPage", "released");//设置跳转页面
		
		List<Columns> columnslis = columnsDao.queryAll();//根据账号获取栏目
		model.add("Columnslis",columnslis);

		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdyfb);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/newsReleaseList.vm";
	}
	
	@Post("getCheckList")//待审核和已发布页面的数据请求
    public String getCheckList(News news, HttpServletRequest request) throws Exception{
		news.setCreateUser(returnSessionObj(request).getAccount());
		List<News> arr = newsDAO.getListByState(news);
		for(News obj : arr){
			obj.setReleaseAt(DateUtils.formatDateStr(obj.getReleaseAt(),DateUtils.DATE_FULL_STR));
		}
		
		JSONObject jo = new JSONObject();
		
		int size = newsDAO.getListSizeByState(news);
		
		jo.put("data", arr);
		jo.put("draw", news.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	@Get("draft")//稿件区
	public String draft(@Param("id") int columnsId,Model model, Flash flash, HttpServletRequest request,@Param("currentPage") int currentPage){
		
		String memu_name = "新闻管理_稿件区_";
		
		flash.add("returnPage", "draft?id=" + columnsId);//设置跳转页面
		flash.add("columnsId", String.valueOf(columnsId));//设置栏目id
		
		model.add("NewsType",EnumSet.allOf(NewsTypeEnum.class));//新闻类型
		model.add("CheckStatus",EnumSet.allOf(NewsCheckStatus.class));//审核状态
		model.add("ReleaseStatus",EnumSet.allOf(NewsReleaseStateEnum.class));//发布状态
		model.add("columnsId",columnsId);
		
		Columns columns = columnsDao.queryById(columnsId);
		
		memu_name = memu_name+columns.getName();
		MemuEnum.lkggaojian.setName(memu_name);
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkggaojian);
        model.add("currentPage",currentPage);//当前页
		return "/views/news/newsDraftList.vm";
	}
	
	@Post("getDraftList")
    public String getDraftList(News news, @Param("beginTime") String begin, @Param("endTime") String end,HttpServletRequest request) throws Exception{
		List<News> arr = newsDAO.getListByColumnsId(news, begin, end);
		for(News obj : arr){
			obj.setCreateAt(DateUtils.formatDateStr(obj.getCreateAt(),DateUtils.DATE_FULL_STR));
			obj.setReleaseAt(DateUtils.formatDateStr(obj.getReleaseAt(),DateUtils.DATE_FULL_STR));
		}
		
		JSONObject jo = new JSONObject();
		
		int size = newsDAO.getListSizeByColumnsId(news,begin, end);
		
		jo.put("data", arr);
		jo.put("draw", news.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@" + jo.toString();
	}
	
	@Get("stick")//置顶
    public String stick(@Param("id") int id, Flash flash, @Param("currentPage")int currentPage) {
		News obj = newsDAO.queryById(id);
		
		if(obj!=null){
			newsDAO.updateByStickTime(id);
		}
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Get("channelStick")//取消置顶
    public String channelStick(@Param("id") int id,  Flash flash, @Param("currentPage")int currentPage) {
		News obj = newsDAO.queryById(id);
		
		if(obj!=null){
			newsDAO.updateByStickTimeIsNull(id);
		}
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Get("top")//设为头条 取消头条
    public String top(@Param("id") int id,  Flash flash,@Param("currentPage")int currentPage) {
		News obj = newsDAO.queryById(id);
		if(obj!=null){
			if(obj.getIsTop() == null || obj.getIsTop().equals(Constants.flag.type1)){
				obj.setIsTop(Constants.flag.type0);
			}
			else{
				obj.setIsTop(Constants.flag.type1);
			}
			newsDAO.updateIsTop(obj);
		}
		
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Get("push")//推送
    public String push(@Param("id") int id) throws IOException {
		News obj = newsDAO.queryById(id);
		if(obj!=null){
			JSONObject content = new JSONObject();
			content.put("newsId", obj.getId());
			content.put("type", obj.getType());
			content.put("title", obj.getTitle());
			GeTui.pushToAll(obj.getTitle(), obj.getAbstractInfor(),content);
			GeTui.transPushToAll(obj.getTitle(), content, true, 1000*600);
		}
		return "r:showNewsSendMsgList";
	}
	
	@Get("comment")//开启评论 禁止评论
    public String comment(@Param("id") int id,  Flash flash, @Param("currentPage")int currentPage) {
		News obj = newsDAO.queryById(id);
		if(obj!=null){
			if(obj.getIsComments() == null || obj.getIsComments().equals(Constants.flag.type1)){
				obj.setIsComments(Constants.flag.type0);
			}
			else{
				obj.setIsComments(Constants.flag.type1);
			}
			newsDAO.updateIsComments(obj);
		}
		
		String returnPage = flash.get("returnPage");
		if(returnPage.indexOf("?") > 0){
			returnPage += "&currentPage="+currentPage;
		}
		else{
			returnPage += "?currentPage="+currentPage;
		}
		return "r:" + returnPage;
	}
	
	@Get("setPV")//设置点击量
	public String setPV(Model model, @Param("id") int id, Flash flash, @Param("currentPage") int currentPage){
		String columnsId = flash.get("columnsId");
		model.add("columnsId",columnsId);
		
		News obj = newsDAO.queryById(id);
		if(obj != null){
			obj.setReleaseAt(DateUtils.getMinusTime(obj.getReleaseAt(), DateUtils.DATE_FULL_STR, 0));
			model.add("obj",obj);
		}
		
		model.add("currentPage",currentPage);
		return "/views/news/newsPVedit.vm";
	}
	
	@Post("updatePV")
	public String updatePV(News news,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			newsDAO.updatePV(news);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
}